package com.xsmart.oa.api;

import com.xsmart.common.entity.EcomException;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.common.services.ApiCallService;
import com.xsmart.oa.services.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class DemoController {
    @Autowired
    DemoService demoService;
    @Autowired
    ApiCallService apiCallService;


    @RequestMapping(value = "/v1/demoController/oa", method = RequestMethod.POST)
    @ResponseBody
    public Object oa(@ModelAttribute RequestBean requestBean, HttpServletRequest request) {
        System.out.println("demo api impl");
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.addResultItem("result", "1");
        return responseBean;
    }

    @RequestMapping(value = "/v1/demoController/queryUserList", method = RequestMethod.POST)
    @ResponseBody
    public Object queryUserList(@ModelAttribute RequestBean requestBean, HttpServletRequest request) throws EcomException {
        System.out.println("demo api impl");
        Object getDemoResponseBean = apiCallService.call("v1", "demoController", "goods", requestBean, request);
        List<Map<String, Object>> userList = demoService.queryUserList();
        ResponseBean responseBean = requestBean.getResponseBean();
        responseBean.addResultItem("result", "1");
        responseBean.addResultItem("userList", userList);
        responseBean.addResultItem("remota", getDemoResponseBean);
        return responseBean;
    }
}
